package com.gobang.modules.room;

import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 房间管理服务
 */
@Slf4j
@Service
public class RoomService {
    
    /** 所有房间 roomId -> Room */
    private final Map<String, Room> rooms = new ConcurrentHashMap<>();
    
    /** 玩家所在的房间 playerId -> roomId */
    private final Map<Long, String> playerRoomMap = new ConcurrentHashMap<>();
    
    /**
     * 创建房间并开始游戏
     */
    public Room createRoom(Long blackPlayerId, String blackPlayerName,
                          Long whitePlayerId, String whitePlayerName) {
        String roomId = IdUtil.simpleUUID();
        Room room = new Room(roomId, blackPlayerId, blackPlayerName,
                            whitePlayerId, whitePlayerName);
        
        rooms.put(roomId, room);
        playerRoomMap.put(blackPlayerId, roomId);
        playerRoomMap.put(whitePlayerId, roomId);
        
        // 立即开始游戏
        room.startGame();
        
        log.info("创建房间并开始游戏: roomId={}, 黑方={}, 白方={}", 
                roomId, blackPlayerName, whitePlayerName);
        
        return room;
    }
    
    /**
     * 结束游戏
     */
    public void endGame(String roomId) {
        Room room = rooms.get(roomId);
        if (room != null) {
            room.endGame();
            log.info("房间游戏结束: roomId={}", roomId);
        }
    }
    
    /**
     * 获取房间
     */
    public Room getRoom(String roomId) {
        return rooms.get(roomId);
    }
    
    /**
     * 获取玩家所在的房间
     */
    public Room getPlayerRoom(Long playerId) {
        String roomId = playerRoomMap.get(playerId);
        return roomId != null ? rooms.get(roomId) : null;
    }
    
    /**
     * 获取玩家所在的房间ID
     */
    public String getPlayerRoomId(Long playerId) {
        return playerRoomMap.get(playerId);
    }
    
    /**
     * 检查玩家是否在房间中
     */
    public boolean isPlayerInRoom(Long playerId) {
        return playerRoomMap.containsKey(playerId);
    }
    
    /**
     * 移除房间
     */
    public void removeRoom(String roomId) {
        Room room = rooms.remove(roomId);
        if (room != null) {
            playerRoomMap.remove(room.getBlackPlayerId());
            playerRoomMap.remove(room.getWhitePlayerId());
            log.info("移除房间: roomId={}", roomId);
        }
    }
    
    /**
     * 玩家离开房间
     */
    public void playerLeaveRoom(Long playerId) {
        String roomId = playerRoomMap.remove(playerId);
        if (roomId != null) {
            Room room = rooms.get(roomId);
            if (room != null) {
                log.info("玩家离开房间: playerId={}, roomId={}", playerId, roomId);
                
                // 如果房间中没有玩家了，移除房间
                if (!playerRoomMap.containsValue(roomId)) {
                    removeRoom(roomId);
                }
            }
        }
    }
    
    /**
     * 获取房间总数
     */
    public int getRoomCount() {
        return rooms.size();
    }
    
    /**
     * 获取正在游戏的房间数
     */
    public int getPlayingRoomCount() {
        return (int) rooms.values().stream()
                .filter(room -> room.getStatus() == RoomStatus.PLAYING)
                .count();
    }
}
